package com.dankegongyu.lib;

import android.os.Bundle;
import android.view.TextureView;

import com.baidu.mapapi.search.core.RouteLine;
import com.dankegongyu.lib.map.baidu.DKMapRoutePlan;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = MainActivity.class.getSimpleName();

    private TextureView mTextureView;
    private DKMapRoutePlan mMapRoutePlan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextureView = new TextureView(this);

        mMapRoutePlan = DKMapRoutePlan.newInstance();
        // 选择好公司后调用路径规划
        routePlan(39.928607, 116.445087, 39.934834, 116.586705);
    }

    private void routePlan(double latFrom, double lngFrom, double latTo, double lngTo) {
        // 跨城公交
        mMapRoutePlan.searchShortestDurationRoute(DKMapRoutePlan.MASS_TRANSIT, latFrom, lngFrom, latTo, lngTo, new DKMapRoutePlan.OnShortestDurationResultListener() {
            @Override
            public void onShortestDurationResult(@NonNull RouteLine shortestRoute) {
                // 公交最短用时
                onRoutePlanResult("公交", shortestRoute.getDuration());
            }
        });
        // 骑行
        mMapRoutePlan.searchShortestDurationRoute(DKMapRoutePlan.BIKING, latFrom, lngFrom, latTo, lngTo, new DKMapRoutePlan.OnShortestDurationResultListener() {
            @Override
            public void onShortestDurationResult(@NonNull RouteLine shortestRoute) {
                // 骑行最短用时
                onRoutePlanResult("骑行", shortestRoute.getDuration());
            }
        });
    }

    private int currentMinDuration = 0;

    private synchronized void onRoutePlanResult(String routeMode, int minDuration) {
        if (currentMinDuration > 0 && minDuration > currentMinDuration) {
            return;
        }
        currentMinDuration = minDuration;
//            DKLog.e("用时最短：" + routeMode + minDuration);
//                tvDuration.setText(routeMode + DKBaiduMapHelper.formatDuration(currentMinDuration));
        // TODO 存SP

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mMapRoutePlan != null) {
            mMapRoutePlan.destroy();
        }
    }
}
